package org.dalmatian.common.redis.config;

import org.dalmatian.common.redis.cache.CacheManager;
import org.dalmatian.common.redis.config.properties.RedissonProperties;
import org.dalmatian.common.redis.manager.PlusSpringCacheManager;
import org.dalmatian.common.redis.supplier.RedissonClientSupplier;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.redisson.api.RedissonClient;


/**
 * redis配置
 *
 * @author Lion Li
 */
@Slf4j
@Configuration
public class RedisConfiguration {

    @Inject
    private RedissonProperties redissonProperties;


    @Bean
    @Condition(onProperty = "${tenant.enable} == false")
    public RedissonClient redissonClient(){
         log.info("redisson config init");
        return new RedissonClientSupplier(redissonProperties).get();
    }
    @Bean
    public Cache<Object, Object> caffeine() {
        return Caffeine.newBuilder()
            // 设置最后一次写入或访问后经过固定时间过期
            .expireAfterWrite(30, TimeUnit.SECONDS)
            // 初始的缓存空间大小
            .initialCapacity(100)
            // 缓存的最大条数
            .maximumSize(1000)
            .build();
    }

    @Bean
    @Condition(onProperty = "${tenant.enable} == false")
    public CacheManager cacheManager(){
        return  new PlusSpringCacheManager();
    }

}
